package com.snow.fkisoftstone.adapter;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.snow.fkisoftstone.R;
import com.snow.fkisoftstone.Utils;
import com.snow.fkisoftstone.bean.WorkTime;

import java.text.DecimalFormat;
import java.util.List;


public class WorkAdapter extends RecyclerView.Adapter<WorkAdapter.ViewHolder> {

    private final static int TYPE_HEAD = 1;
    private final static int TYPE_ONTENT = 2;
    private final static int TYPE_BOTTOM = 3;

    private List<WorkTime> mWorkList;

    private int dayCount = 0;
    private float total;
    private float ave;

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvItemIndex;
        TextView tvItemDay;
        TextView tvItemWeek;
        TextView tvItemStartTime;
        TextView tvItemEndTime;
        TextView tvItemWorkTime;

        public ViewHolder(View view) {
            super(view);
            tvItemIndex = (TextView) view.findViewById(R.id.tvIndex);
            tvItemDay = (TextView) view.findViewById(R.id.tvDay);
            tvItemWeek = (TextView) view.findViewById(R.id.tvWeek);
            tvItemStartTime = (TextView) view.findViewById(R.id.tvStartTime);
            tvItemEndTime = (TextView) view.findViewById(R.id.tvEndTime);
            tvItemWorkTime = (TextView) view.findViewById(R.id.tvWorkTime);
        }

    }

    public WorkAdapter(List<WorkTime> workList) {
        mWorkList = workList;
        generateData();
    }

    public void setmWorkList(List<WorkTime> mWorkList) {
        this.mWorkList = mWorkList;
        generateData();
    }

    private void generateData() {
        if (mWorkList != null && mWorkList.size() > 0) {
            dayCount = 0;
            total = 0;
            for (WorkTime w : mWorkList) {
                if (!TextUtils.isEmpty(w.getStartTimeStr()) && !TextUtils.isEmpty(w.getEndTimeStr())) {
                    total += (w.getWorkTime() == 0L ? Utils.getWorkTime(w.getStartTimeStr(), w.getEndTimeStr()) : w.getWorkTime());
                    dayCount++;
                }
            }
            ave = total / dayCount;
        } else {
            dayCount = 0;
            ave = 0;
            total = 0;
        }
    }

    @Override

    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_work, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (holder.getItemViewType() == TYPE_BOTTOM) {
            holder.tvItemDay.setText("");
            holder.tvItemWeek.setText("");
            holder.tvItemStartTime.setText("");
            holder.tvItemIndex.setText(dayCount + "");
            holder.tvItemEndTime.setText(new DecimalFormat(".00").format(ave));
            holder.tvItemWorkTime.setText(new DecimalFormat(".00").format(total));
            return;
        }else if (holder.getItemViewType()==TYPE_HEAD){
            return;
        }
        WorkTime workTime = mWorkList.get(position-1);
        if (workTime.getIndex() == 0)
            holder.tvItemIndex.setText(Integer.parseInt(workTime.getDay().split("-")[2]) + "");
        else
            holder.tvItemIndex.setText(workTime.getIndex() + "");
        holder.tvItemDay.setText(workTime.getDay());
        holder.tvItemWeek.setText(workTime.getWeek());
        holder.tvItemStartTime.setText(workTime.getStartTimeStr());
        holder.tvItemEndTime.setText(workTime.getEndTimeStr());
        if (!TextUtils.isEmpty(workTime.getEndTimeStr()) && workTime.getWorkTime() == 0F) {
            holder.tvItemWorkTime.setText(new DecimalFormat(".00").format(
                    Utils.getWorkTime(workTime.getStartTimeStr(), workTime.getEndTimeStr())
            ));
        } else
            holder.tvItemWorkTime.setText(new DecimalFormat(".00").format(workTime.getWorkTime()));
    }

    @Override
    public int getItemCount() {
        if (mWorkList==null)
            return 1;
        return mWorkList.size() + 2;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_HEAD;
        } else if (position == mWorkList.size()+1) {
            return TYPE_BOTTOM;
        } else {
            return TYPE_ONTENT;
        }
    }


}